package com.example.controller;

import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/flowable")
public class FlowableController {
	
	@Resource
	private ProcessEngine processEngine;

	@GetMapping("/deploy")
	public String deploy() {
		 // 部署流程 获取RepositoryService对象
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deployment = repositoryService
                .createDeployment()     
                .addClasspathResource("bpmnfile/holiday-request.bpmn20.xml") // 指定流程定义文件
                .name("请假流程")   // 流程名称
                .deploy();  // 执行部署

        log.info("流程部署成功，请查看在act_re_deployment、act_re_procdef、act_ge_bytearray表记录");
        log.info("deployment id is: {}",deployment.getId());
        log.info("deployment name is: {}",deployment.getName());
		return "hello world";
	}
	
	@GetMapping("/start")
	public String start() {
		RuntimeService  runtimeService = processEngine.getRuntimeService();
    	ProcessInstance inst = runtimeService.startProcessInstanceByKey("holidayRequest");
    	log.info("stime={}",inst.getStartTime());
		return "hello world";
	}
}
